package com.suxin.test.netty.adv.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.example.netty.adv.server.async.DefaultITaskProcessor;
import org.example.netty.adv.server.async.ITaskProcessor;

/**
 * @Author: tang
 * @Description: ServerInit
 * @Date 2025/1/21 14:34
 */
public class ServerInit extends ChannelInitializer<SocketChannel> {

    private final ITaskProcessor processor = new DefaultITaskProcessor();

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ch.pipeline()
                .addLast(new LoggingHandler())
                .addLast(new ReadTimeoutHandler(3600))
                .addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
                        0,2,0,2))
                .addLast(new LengthFieldPrepender(2))
                .addLast(new KryoEncoder())
                .addLast(new KryoDecoder())
                .addLast(new ServerHeartbeatBoundChannel())
                .addLast(new ServerBusiInBoundChannelHandler(processor))
        ;
    }
}
